package springboot.example.tools;

import org.springframework.beans.factory.annotation.Autowired;
import springboot.example.domain.OracleProperties;

import java.sql.*;

public class OracleDBUtil {

    @Autowired
    private static OracleProperties oracleProperties;

    public static Connection getConnection(){

        System.err.print(oracleProperties.toString());

        Connection connection=null;
        try {
            Class.forName(oracleProperties.getDriverName());
            String url=oracleProperties.getUrl();
            String username=oracleProperties.getUsername();
            String password=oracleProperties.getPassword();
            connection=DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }


    public static void close(Connection connection,PreparedStatement pstmt,ResultSet rs){
        close(rs);
        close(pstmt);
        close(connection);
    }

    public static void close(Connection connection,Statement sm,ResultSet rs){
        close(rs);
        close(sm);
        close(connection);
    }

    private static void close(Connection connection){
        if(connection !=null){
            try {
                connection.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }

    private static void close(Statement sm){
        if(sm !=null){
            try {
                sm.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }
    private static void close(PreparedStatement pstmt){
        if(pstmt !=null){
            try {
                pstmt.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
        }
    }

    private static void close(ResultSet rs){
        if(rs !=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
